﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     CIS  Domestic CategoryManagementHelper
// Created By:    Samara Jiang (samaraj@wicresoft.com)
// Created Date:  2011-1-30
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    /// <summary>
    /// Category Management模块数据库访问类
    /// </summary>
    public static class CategoryManagementHelper
    {

        /// <summary>
        /// 通过CategoryID查询Departments
        /// </summary>
        public static IEnumerable GetDepartmentWithCategoryRelation(int cateID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var category = dc.Categories.FirstOrDefault(p => p.CateID == cateID);
                var department = dc.Departments.Where(p => p.Disabled == false || !p.Disabled.HasValue).ToArray();
                category.Departments.Load();
                var source = from p in department
                             select new
                             {
                                 p.DepartmentID,
                                 p.DepartmentNameCN,
                                 p.DepartmentNameEN,
                                 p.Description,
                                 ContainsProvince = category.Departments.Contains(p) ? "checked" : "",
                             };
                return source.ToArray();
            }
        }


        /// <summary>
        /// CAT与现有所有省的ID绑定入PCAT表
        /// </summary>
        public static void OnCategoryInserting(string name, string description)
        {
            using (var dc = new CCEntities())
            {
                Category category = new Category();
                category.Name = name;
                category.Description = description;
                dc.AddToCategories(category);
                dc.SaveChanges();
                var Provinces = dc.Provinces.Where(p => !p.Disabled).ToArray();
                foreach (var pro in Provinces)
                {
                    PCAT pcat = new PCAT() { Province = pro, Category = category };
                    dc.AddToPCATs(pcat);
                    Role role = new Role();
                    role.IsAdmin = false;
                    role.Name = pro.ProvinceID.ToString() + ";" + category.CateID.ToString();
                    role.Description = "+pcat+";
                    role.BelongsTo = "CID";
                    role.PCATs.Add(pcat);
                    dc.AddToRoles(role);
                    category.PCATs.Add(pcat);
                }
                dc.SaveChanges();
            }
          
        }



        /// <summary>
        /// 为Category修改Departments
        /// </summary>
        public static void OnCategoryUpdating(ObjectContext dc, Category category, string departmentList)
        {
            category.Departments.Load();
            category.Departments.Clear();
            CategoryInserting(dc, category, departmentList);

        }

        private static void CategoryInserting(ObjectContext dc, Category category, string departmentList)
        {
            var departments = dc.CreateQuery<Department>("[Departments]").Where(p => p.Disabled == false || !p.Disabled.HasValue);
            if (!String.IsNullOrEmpty(departmentList))
            {

                foreach (var department in departmentList.Split(','))
                {
                    int departmentID = int.Parse(department);

                    var DepartmenyInCategory = departments.First(p => p.DepartmentID == departmentID);

                    category.Departments.Add(DepartmenyInCategory);

                }

            }


        }
        /// <summary>
        /// 将一个category的激活状态设为未激活
        /// </summary>
        public static void SetCategoryDisabled(int cateID)
        {
            using (var dc = new CCEntities())
            {
                dc.Categories.FirstOrDefault(p => p.CateID == cateID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前ID获取Name
        /// </summary>
        public static IEnumerable getName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.Categories.FirstOrDefault(p => p.CateID == entityID).Name;
                return name;
            }
        }

    }
}
